package com.whitedust.top70;

import com.whitedust.structure.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author A
 * @since 2025/11/11 12:54
 */

public class Top61 {
    int sum = 0;

    public int sumNumbers(TreeNode root) {
        StringBuilder path = new StringBuilder();
        if (root == null) return sum;
        path.append(root.val);
        leafNode(root, path);
        return sum;
    }

    private void leafNode(TreeNode root, StringBuilder path) {
        if (root.left == null && root.right == null) {
            sum += Integer.parseInt(path.toString());
            return;
        }
        if (root.left != null) {
            path.append(root.left.val);
            leafNode(root.left, path);
            path.deleteCharAt(path.length() - 1);
        }
        if (root.right != null) {
            path.append(root.right.val);
            leafNode(root.right, path);
            path.deleteCharAt(path.length() - 1);
        }
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(3);
        a.left = b;
        a.right = c;
        new Top61().sumNumbers(a);

    }
}
